package com.showde.shoppingmall.business.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.kim.net.NetEntity;
import com.showde.shoppingmall.business.NetCallbackImplBase.Notify;
import com.showde.shoppingmall.business.ProductSearchBusiness;
import com.showde.shoppingmall.domain.SearchResult;

public class ProductSearchLogic {

	private static final String TAG = "ProductSearchLogic";

	public static void searchProduct(final Context context,
			String productCategory, String searchKey,
			final UICallbackInterface uiCallbackInterface) {
		ProductSearchBusiness searchBusiness = new ProductSearchBusiness(
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						processResult(context, uiCallbackInterface, netEntity);
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						processError(errorMsg, uiCallbackInterface);
					}
				}, productCategory, searchKey);
		searchBusiness.doBusiness();
	}

	public static void searchProduct(final Context context, String searchKey,
			final UICallbackInterface uiCallbackInterface) {
		ProductSearchBusiness searchBusiness = new ProductSearchBusiness(
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						processResult(context, uiCallbackInterface, netEntity);
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						processError(errorMsg, uiCallbackInterface);
					}
				}, searchKey);
		searchBusiness.doBusiness();
	}

	/**
	 * 处理错误
	 */
	private static final void processError(String errorMsg,
			UICallbackInterface uiCallbackInterface) {
		Log.d(TAG, "errorMsg:" + errorMsg);
		uiCallbackInterface.onError(errorMsg);
	}

	/**
	 * 获取数据成功，处理结果
	 * 
	 * @param context
	 * @param uiCallbackInterface
	 * @param netEntity
	 */
	private static final void processResult(final Context context,
			final UICallbackInterface uiCallbackInterface, NetEntity netEntity) {
		JSONObject jsonObject = netEntity.result;
		try {
			if (!"0".equals(jsonObject.getInt("ReturnCode")+"")) {
				uiCallbackInterface.onError("fail::" + netEntity.receiveData);
				return;
			}

			JSONArray jsonArray = jsonObject.getJSONArray("List");
			if (jsonArray != null && jsonArray.length() > 0) {
				SearchResult searchResult = null;
				List<SearchResult> searchResults = new ArrayList<SearchResult>();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject arrayItem = jsonArray.getJSONObject(i);
					searchResult = new SearchResult();
					searchResult.pID = arrayItem.getInt("ID") + "";
					searchResult.pName = arrayItem.getString("NAME");
					searchResult.pDesc = arrayItem.getString("TITLE");
					searchResult.pPrice = arrayItem.getDouble("PRICE")
							+ "";
					searchResult.pLogo = arrayItem.getString("LOGO");
					searchResults.add(searchResult);
				}
				CallbackData data = new CallbackData();
				data.data = searchResults;
				uiCallbackInterface.onSuccess(data);
				return ;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		uiCallbackInterface.onError("receiveData:" + netEntity.receiveData);
	}

}
